package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl implements AdChannelService {
    @Autowired
    private AdChannelMapper adChannelMapper;
    @Override
    public PageResponseResult findPage(ChannelDto channelDto) {
        // 判断  dto == null 证明传输对象为空  报错  当前代码的逻辑  只判断错 不判断对！
        if (channelDto == null) {
            // 直接返回
            return (PageResponseResult)PageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 初始化分页
        channelDto.checkParam();
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //构建条件
        lambdaQueryWrapper.like(channelDto.getName()!=null&&!"".equals(channelDto.getName()),
                AdChannel::getName,channelDto.getName());

        Page<AdChannel> page = new Page<>(channelDto.getPage(),channelDto.getSize());
        IPage<AdChannel> ipage = adChannelMapper.selectPage(page,lambdaQueryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(channelDto.getPage(),
                channelDto.getSize(),Integer.valueOf(ipage.getTotal()+""));
        pageResponseResult.setData(ipage.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult save(AdChannel adChannel) {
        if(adChannel==null){
            // 直接返回
            return (PageResponseResult)PageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 自维护 创建时间
        adChannel.setCreatedTime(new Date());
        adChannelMapper.insert(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findById(Integer id) {
        if(id==null){
            //直接返回
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel adChannel = adChannelMapper.selectById(id);
        return ResponseResult.okResult(adChannel);
    }

    @Override
    public ResponseResult edit(Integer id, AdChannel adChannel) {
        if(adChannel==null||id==null){
            //直接返回
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据名称做为空校验
        if(adChannel.getName()==null || "".equals(adChannel.getName().trim())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //自维护 创建时间
        adChannel.setCreatedTime(new Date());
        adChannelMapper.updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
